package com.santiao.fuel.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

//import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.santiao.publics.ui.SettingFragment;
import com.santiao.fuel.R;
import com.santiao.fuel.databinding.ActivityCalculateBinding;
import com.santiao.fuel.ui.widget.BaseActivity;
import com.santiao.fuel.utils.PowerPrice;

import java.text.DecimalFormat;
import java.util.Map;

public class CalculateActivity extends BaseActivity {
    ActivityCalculateBinding binding;
    double price=0.4883;
    double dl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);
        binding= DataBindingUtil.setContentView(this,R.layout.activity_calculate);
        setToolbar(R.id.toolbar_power,R.id.title_power,"电费价格");
        Map<String, Double> powerPrice = PowerPrice.getPowerPrice();
        String[] cityNames = powerPrice.keySet().toArray(new String[0]);
        binding.constraintLayout2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 这种弹窗从 1.0.0版本开始实现了优雅的手势交互和智能嵌套滚动
                new XPopup.Builder(CalculateActivity.this)
                        .maxHeight(1000)
                        .asBottomList("请选择城市", cityNames,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                      binding.textView2.setText(text);
                                      price=powerPrice.get(text);
                                      if (binding.edit.getText().toString().isEmpty())
                                          add(0);
                                      else {
                                          add( Double.parseDouble(binding.edit.getText().toString()));
                                      }

                                    }
                                })
                        .show();
            }
        });
        binding.edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().isEmpty()){
                    dl=Double.parseDouble(s.toString());
                    add(dl);
                }else {
                    add(0.0);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        SettingFragment.adLoad(1,null,null,this,this);
    }

    private void add(double dl) {
        double sum=dl*price;
        DecimalFormat df = new DecimalFormat("#0.00");
        binding.sum.setText(df.format(sum));
    }
}